﻿$(function(){//延迟加载
	var url='';
	var oltip='';
	var oltId='';
	var oltname='';
	var jfId='';
	var done=1;//是否编辑结束
	var crows=null;
	$('#desearch').panel({
		iconCls:'icon-search',
		collapsible:true,
		title:'机房查询',
		width:'100%',
		height:'70px'
	});
	
	
//	$('#b1').linkbutton({
//		iconCls:'icon-add',
//		plain:true,
//		onClick:function(){
//			append();
//		}
//	});
//	$('#b2').linkbutton({
//		iconCls:'icon-edit',
//		plain:true,
//		onClick:function(){
//		removeit();
//		}
//	});
	$('#b1').linkbutton({
		iconCls:'icon-add',
//		plain:true,
		width:'150px',
		onClick:function(){
			parent.addTab('新增设备','toaddolt');
		}
	});
	
	$('#b3').linkbutton({
		iconCls:'icon-save',
//		plain:true,
		width:'150px',
		onClick:function(){
		accept();
		}
	});
	$('#b4').linkbutton({
		iconCls:'icon-redo',
		onClick:function(){
			var oltrow=$('#oltsec').datagrid('getSelected');
			parent.addTab('编辑上联','toeditparent?dvId='+oltrow.oltId+'&dvbelong=olt');
		}
	});
	$('#b5').linkbutton({
		iconCls:'icon-redo',
		onClick:function(){
			var oltrow=$('#oltsec').datagrid('getSelected');
			parent.addTab('OLT设备PON口配置','toconfigoltpon?oltId='+oltrow.oltId);
		}
	});
	$('#b6').linkbutton({
		iconCls:'icon-add',
		onClick:function(){
			var oltrow=$('#oltsec').datagrid('getSelected');
			parent.addTab('OLT-PON口ODF配置','toconfigoltodf?oltId='+oltrow.oltId);
		}
	});
	$('#oltsec').datagrid({
		iconCls: 'icon-edit',
		singleSelect: true,
		toolbar:'#tb',
		url: 'showoltlist',
		queryParams:{
			jfId:'',
			oltip:'',
			dvfirmId:'',
			dvmodelId:''
		},
		
		method: 'get',
		title:'Olt列表',
		height:'550px',
		pageSize:20,
		onDblClickRow: onClickRow,
		pagination:true,
		rownumbers:true,
		columns:[[{
			field:'oltname',width:'15%',title:'设备名称',editor:{
	        	  type:'textbox',
	        		 options:{
	        			 required:true
	        		 }
	          }
		},{
			field:'oltip',width:'7%',title:'设备IP'
		},{
			field:'juxiang',width:'5%',title:'区局'
		},{
			field:'jfname',width:'8%',title:'设备机房'
		},{
			field:'oltfirm',width:'5%',title:'设备厂商'
		},{
			field:'modelname',width:'6%',title:'设备型号'
		},{
			field:'oltnum',width:'3%',title:'编号'
		},{
			field:'oltpppv',width:'7%',title:'pppoe vlan'
		},{
			field:'oltpontype',width:'5%',title:'设备类型'
		},{
			field:'voiprange',width:'15%',title:'语音地址段'
		},{
			field:'oltponnum',width:'4%',title:'pon口数'
		},{
			field:'oltuseponnum',width:'4%',title:'可pon口'
		},{
			field:'oltremark',width:'5%',title:'备注',editor:{
	        	  type:'textbox',
	        		 options:{
	        			 //required:true 
	        		 }
	          }
		},{
			field:'creatdate',width:'8%',title:'录入',editor:'datetimebox',
			formatter:function(value,row,index){
        		if(value!=null&&value!=''){
        			var date = new Date(value);
        			Y = date.getFullYear() + '-';
        			M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        			D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
        			h =(date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
        			m =(date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes()) + ':';
        			s =(date.getSeconds()< 10 ? '0'+(date.getSeconds()) : date.getSeconds()); 
        			return Y+M+D+h+m+s;
        		}else{
        			return '';
        		}
        	}
		}]],
		onLoadSuccess:function(){
			$('#ceshi').linkbutton({
				width:'300px',
				text:'上联OLT：'+$('#onusec').datagrid('getRows')[0].oltname+'('+$('#onusec').datagrid('getRows')[0].oltip+')',
				onClick:function(){
					var rows=$('#onusec').datagrid('getRows');
					alert(rows[0].onuId);
				}
			})
		}	
	});
	
	$('#oltipsec').textbox({
		buttonText:'搜索',
		buttonIcon:'icon-search',
		prompt:'OLT IP...',
		onClickButton:function(){
			if($('#oltipsec').val()!='')
			$('#oltsec').datagrid('reload',{
				oltip:$('#oltipsec').val(),
				dvfirmId:'',
				dvmodelId:'',
				jfId:''
			});
		}
	});
	
	$('#dvmodel').combobox({
		width:'150px',
		disabled:true,
		onChange:function(newValue,oldValue){
			if(newValue!=''){				
				$('#oltsec').datagrid('reload',{
					oltip:'',
					dvfirmId:'',
					dvmodelId:newValue,
					jfId:''
				});
			}
		}
	});
	
	$('#dvfirm').combobox({
		onChange:function(newValue,oldValue){
			if(newValue==''){				
				$('#dvmodel').combobox({
					disabled:true
				});
			}else{
				$('#dvmodel').combobox({
					//value:'请选择',
					disabled:false,
					valueField:'dvmodelId',
					textField:'modelname',
					url:"showmodelsec?dvbelong=olt&dvfirmId="+newValue,
				});
			}
		}
	});
	
	
	var pager = $('#oltsec').datagrid('getPager');	// get the pager of datagrid
	pager.pagination({
		buttons:[{
			iconCls:'icon-ok',
			text:'保存修改',
			width:'150px',
			handler:function(){
				if(done==0){
					alert('请先确认修改');
					return;
				}				
				alert(crows.length);
				if(crows.length==0){
					return;
				}
				
				$.ajax({
					url:'updateoltrows',
					type:'POST',
					data:{
						oltrows:JSON.stringify(crows)
					},
					success:function(data){
						alert(data.msg);
					}
				});
			}
		},{
			iconCls:'icon-edit',
			text:'编辑下联设备',
			width:'150px',
			handler:function(){
				var secrow=$('#oltsec').datagrid('getSelected');
				if(secrow!=null){
					oltname=secrow.oltname;
					oltId=secrow.oltId;
					parent.addTab(oltname,'toonuadmin?oltId='+oltId);
				}
			}
		},{
			iconCls:'icon-zzjg',
			text:'设备详细拓扑',
			width:'150px',
			handler:function(){
				var secrow=$('#oltsec').datagrid('getSelected');
//				alert(secrow);
				if(secrow!=null){				
					oltip=secrow.oltip;
					oltname=secrow.oltname;
					oltId=secrow.oltId;
					parent.addTab(oltname+'('+oltip+')','tojxtp?dvIds=olt,'+oltId);	
				}
			}
		},{
			iconCls:'icon-edit',
			text:'详细信息编辑',
			width:'150px',
			handler:function(){
				var secrow=$('#oltsec').datagrid('getSelected');
				var oltname=secrow.oltname;
				oltId=secrow.oltId;
				parent.addTab(oltname,'tooltedit?oltId='+oltId);
			}
		}]
	});
	
//	$('#ceshi').linkbutton({
//		width:'100px',
//		onClick:function(){
//			var rows=$('#onusec').datagrid('getRows');
//			alert(rows[0].onuId);
//		}
//	})
	$('#osec').load('showjifangsec',function(){
		$.getScript('/xasj/js/component/jifangsec.js');
		$('#juxiang').combobox({
			height:'30px'
		});
		$('#dajuxiang').combobox({
			height:'30px'
		})
		$('#jifang').combobox({
			height:'30px',
			onChange:function(newValue,oldValue){
				$('#oltsec').datagrid('reload',{
					jfId:newValue,
					oltip:'',
					dvfirmId:'',
					dvmodelId:''
				});
			}
		});
	});
	
	var editIndex = undefined;	
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#oltsec').datagrid('validateRow', editIndex)){
			//var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'productid'});
			//var productname = $(ed.target).combobox('getText');
			//$('#dg').datagrid('getRows')[editIndex]['productname'] = productname;
			//$('#dg').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}

	function onClickRow(index){
		done=0;
		if (editIndex!= index){
			if (endEditing()){
				$('#oltsec').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#oltsec').datagrid('selectRow', editIndex);
			}
		}
	}

	function append(){
		if (endEditing()){
			var frows=$('#oltsec').datagrid('getSelected');
			if(frows==''||frows==null)
			alert('未选中行');
			$('#dg').datagrid('appendRow',
				{
				type:'P',
				dvname:frows.dvname,
				dvip:frows.dvip,
				dvbelong:frows.dvbelong,
				dvId:frows.dvId,
				parentname:frows.parentname,
				parentip:frows.parentip,
				parentbelong:frows.parentbelong,
				parentId:frows.parentId
				});
			editIndex = $('#oltsec').datagrid('getRows').length-1;
			$('#oltsec').datagrid('selectRow', editIndex)
					.datagrid('beginEdit', editIndex);
			}
		}
	
	function removeit(){
		if (editIndex == undefined){return}
		var drow=$('#oltsec').datagrid('getRows')[editIndex];
		//alert(drow.dvrelaId);
		if(drow.dvrelaId==undefined|| drow.dvrelaId==''){			
			return
		}
		$.ajax({
			type:'POST',
			url:'deletedvrelarow',
			data:{
				dvslot:drow.dvslot,
				parentslot:drow.parentslot,
				dvrelaId:drow.dvrelaId
			},
			success:function(data){
				$('#oltsec').datagrid('cancelEdit', editIndex)
				.datagrid('deleteRow', editIndex);
				alert(data.msg);				
			}
		});
	}

	function accept(){
		if (endEditing()){
			done=1;
			crows = $('#oltsec').datagrid('getChanges','updated');
			$('#oltsec').datagrid('acceptChanges');
			editIndex = undefined;
		}
	}

	function reject(){
		$('#oltsec').datagrid('rejectChanges');
		editIndex = undefined;
	}

	function getChanges(){
		var rows = $('#oltsec').datagrid('getChanges');
		alert(rows.length+' rows are changed!');
	}
	
	$('#comvalue').linkbutton({
		iconCls:'icon-ok',
		text:'测试COM',
		onClick:function(){
			var comvalue=$('#dvmodel').combobox('getValue');
			alert(comvalue);
			if(comvalue=='')
				alert('kong');
		}
	});

});